<?php
class Login extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->library(array('session', 'pagination'));
		$this->load->helper("url");
		$this->load->database();
		session_start();
	}
	
	function index(){
 		$ok = $this->session->userdata('admin');
 		if(!isset($ok)||$ok! = 'ok'){
 			redirect("login/denglu");
 		}else{
			$this->load->view("admin/index");
 		}
	}
	
	function check_login(){

		$username = $this->input->post("username");
		$patterns = array(); 
		$patterns[0] = '/and/';     
		$patterns[1] = '/or/';
		$patterns[2] = '/`/';
		$replacements = array();
		$replacements[2] = '_';
		$replacements[1] = '_';
		$replacements[0] = '_';   //字符串替换，将and，or，`等特殊的字符替换掉，防止sql注入
		$username = preg_replace($patterns, $replacements, $username); 
		$username = str_replace("'", "\'", $username);		
		//exit($username);

		$password = $this->input->post("password");
		$true_yanzheng = $_SESSION['authcode'];   	//获得验证码
		$yanzheng = $this->input->post("check");

		$this->load->model("admin_model");
		$admin_message = $this->admin_model->get_admin_message($username);
		foreach($admin_message as $value){
			$id = $value->id;
			$username1 = $value->username;
			$password1 = $value->password;
		}
		if(empty($admin_message)){
			$data['error'] = "用户名错误！";
			$this->load->view("admin/login", $data);
		}else if(md5($password) != $password1){
			$data['error'] = "密码错误！";
			$this->load->view("admin/login", $data);
		}else if($true_yanzheng != $yanzheng){
			$data['error'] = "验证码错误！";
			$this->load->view("admin/login", $data);
		}else{
			$this->session->set_userdata("id", $id);
			$this->session->set_userdata("username", $username1);
			$this->session->set_userdata("admin", 'ok');
			$_SESSION['authcode'] = "";

			redirect("login/index");
		}
	}
	
	function denglu(){
		$this->load->view("admin/login");
	}
	
	function modify(){
		$name = $this->session->userdata('username');
		$this->load->model("admin_model");
		$data["message"] = $this->admin_model->get_admin_message($name);
		$this->load->view("admin/change_info", $data);
	}
	
	function check_modify(){
		$id = $this->input->post("id");
		$name = $this->input->post("username");
		$this->load->model("admin_model");
		$data["message"] = $this->admin_model->get_admin_message($name);
		$result = $this->admin_model->updata_message($id);
		if($result == true){
			$data['error'] = "修改成功！";
			$this->load->view("admin/change_info", $data);
		}else{
			$data['error'] = "修改失败";
			$this->load->view("admin/change_info", $data);
		}
	}
	
	function quit(){
		$this->session->sess_destroy();   //销毁session
		$this->load->view("admin/login");
	}
	
	function top(){
		$data['name'] = $this->session->userdata('username');
		$this->load->view("admin/top", $data);
	}
	
	function left(){
		$this->load->view("admin/left");
	}
	
	function right(){
		$this->load->view("admin/right");
	}

/*
*	register
*/
	function register(){


		$this->load->view("admin/register");
	}

	function register_in(){
		$this->load->model("admin_model");

		$username = trim($this->input->post("username"));
		$patterns = array(); 
		$patterns[0] = '/and/';     
		$patterns[1] = '/or/';
		$patterns[2] = '/`/';
		$replacements = array();
		$replacements[2] = '_';
		$replacements[1] = '_';
		$replacements[0] = '_';   //字符串替换，将and，or，`等特殊的字符替换掉，防止sql注入
		$username = preg_replace($patterns, $replacements, $username); 
		$username = str_replace("'", "\'", $username);		
		//var_dump($username);

		$password = trim($this->input->post("password"));
		$repassword = trim($this->input->post("repassword"));
		$true_yanzheng = $_SESSION['authcode'];   //获得验证码
		$yanzheng = $this->input->post("check");
		$user_length = mb_strlen($username, 'utf8');

		$admin_message = $this->admin_model->get_admin_message($username);
		$check_exist = false;
		foreach($admin_message as $value){
			if ($username == $value->username) {
				$check_exist = true;
			}
		}

		if( $user_length > 20 || $user_length < 5){
			$data['error'] = "用户名要在5-20个字符之间";
			$this->load->view("admin/register", $data);
		}else if( $check_exist ){
			$data['error'] = "用户名已存在！";
			$this->load->view("admin/register", $data);
		}else if( mb_strlen($password, 'utf8') < 3 ){
			$data['error'] = "密码太短！";
			$this->load->view("admin/register", $data);
		}else if($password != $repassword){
			$data['error'] = "两次密码不一致！";
			$this->load->view("admin/register", $data);
		}else if($true_yanzheng != $yanzheng){
			$data['error'] = "验证码错误！";
			$this->load->view("admin/register", $data);
		}else{
			$this->admin_model->save_user($username, md5($password));
			$data['error'] = "注册成功！";
			
			$this->load->view("admin/login", $data);
		}
	}


}